﻿Public Class MonStokSaatIniPage

    Private Sub MonStokSaatIniPage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TreeGrid1.ProcName = New String() {"proc_searchallEqual", "proc_searchallEqual"}
        ComCariBy.SelectedIndex = 0
        setgrid()
    End Sub
    Private Sub setgridv()
        Dim obj() As Object
        Dim param As New List(Of Object)
        If ComCariBy.Text = "Menu" Then
            param.Clear()
            obj = New Object() {"*", "vstokmenufix", "iditemmenu", ComId.SelectedValue}
            param.Add(obj)
            obj = New Object() {"*", "vstokmenua", "iditemmenu", ComId.SelectedValue}
            param.Add(obj)

            TreeGrid1.Params = param.ToArray
            TreeGrid1.displaycolumn.Clear()
            TreeGrid1.displaycolumn.Add("iditemmenu")
            TreeGrid1.displaycolumn.Add("namaitemmenu")
            'TreeGrid1.displaycolumn.Add("namaitem")
            TreeGrid1.displaycolumn.Add("qty")
            TreeGrid1.displaycolumn.Add("stok")
            TreeGrid1.displaycolumn.Add("idproduk")
            TreeGrid1.displaycolumn.Add("namaproduk")
            TreeGrid1.displaycolumn.Add("qtydetail")


            TreeGrid1.setGridPproperty("iditemmenu")
        ElseIf ComCariBy.Text = "Produk" Then
            param.Clear()
            obj = New Object() {"*", "vstokprodukfix", "idproduk", ComId.SelectedValue}
            param.Add(obj)
            obj = New Object() {"*", "vstokproduka", "idproduk", ComId.SelectedValue}
            param.Add(obj)

            TreeGrid1.Params = param.ToArray
            TreeGrid1.displaycolumn.Clear()
            TreeGrid1.displaycolumn.Add("idproduk")
            TreeGrid1.displaycolumn.Add("namaproduk")
            'TreeGrid1.displaycolumn.Add("namaitem")
            TreeGrid1.displaycolumn.Add("qty")
            TreeGrid1.displaycolumn.Add("stok")
            TreeGrid1.displaycolumn.Add("iditem")
            TreeGrid1.displaycolumn.Add("namaitem")
            TreeGrid1.displaycolumn.Add("qtyitem")
            TreeGrid1.displaycolumn.Add("satuan")


            TreeGrid1.setGridPproperty("idproduk")
        ElseIf ComCariBy.Text = "Item" Then
            param.Clear()
            obj = New Object() {"*", "vitem", "iditem", ComId.SelectedValue}
            param.Add(obj)
            obj = New Object() {"*", "vstokaktif", "iditem", ComId.SelectedValue}
            param.Add(obj)

            TreeGrid1.Params = param.ToArray
            TreeGrid1.displaycolumn.Clear()
            TreeGrid1.displaycolumn.Add("groupitem")
            TreeGrid1.displaycolumn.Add("namagroupitem")
            'TreeGrid1.displaycolumn.Add("namaitem")
            'TreeGrid1.displaycolumn.Add("qty")
            'TreeGrid1.displaycolumn.Add("stok")
            TreeGrid1.displaycolumn.Add("iditem")
            TreeGrid1.displaycolumn.Add("namaitem")
            TreeGrid1.displaycolumn.Add("qtyakhir")
            TreeGrid1.displaycolumn.Add("satuanjual")


            TreeGrid1.setGridPproperty("groupitem")
        End If
    End Sub
    Private Sub setgrid()
        Dim obj() As Object
        Dim param As New List(Of Object)
        If ComCariBy.Text = "Menu" Then
            param.Clear()
            If ComId.SelectedValue.ToString = "-1" Or ComId.SelectedValue.ToString Is Nothing Then
                obj = New Object() {"*", "vstokmenufix", "namaitemmenu", ""}
                param.Add(obj)
                obj = New Object() {"*", "vstokmenua", "namaitemmenu", ""}
                param.Add(obj)
                TreeGrid1.ProcName(0) = "proc_searchall"
                TreeGrid1.ProcName(1) = "proc_searchall"
            Else
                obj = New Object() {"*", "vstokmenufix", "iditemmenu", ComId.SelectedValue}
                param.Add(obj)
                obj = New Object() {"*", "vstokmenua", "iditemmenu", ComId.SelectedValue}
                param.Add(obj)
                TreeGrid1.ProcName(0) = "proc_searchallEqual"
                TreeGrid1.ProcName(1) = "proc_searchallEqual"
            End If
            

            TreeGrid1.Params = param.ToArray
            TreeGrid1.displaycolumn.Clear()
            TreeGrid1.displaycolumn.Add("iditemmenu")
            TreeGrid1.displaycolumn.Add("namaitemmenu")
            'TreeGrid1.displaycolumn.Add("namaitem")
            TreeGrid1.displaycolumn.Add("qty")
            TreeGrid1.displaycolumn.Add("stok")
            TreeGrid1.displaycolumn.Add("idproduk")
            TreeGrid1.displaycolumn.Add("namaproduk")
            TreeGrid1.displaycolumn.Add("qtydetail")


            TreeGrid1.setGridPproperty("iditemmenu")
        ElseIf ComCariBy.Text = "Produk" Then
            param.Clear()
           

            If ComId.SelectedValue.ToString = "-1" Or ComId.SelectedValue.ToString Is Nothing Then
                obj = New Object() {"*", "vstokprodukfix", "namaproduk", ""}
                param.Add(obj)
                obj = New Object() {"*", "vstokproduka", "namaproduk", ""}
                param.Add(obj)
                TreeGrid1.ProcName(0) = "proc_searchall"
                TreeGrid1.ProcName(1) = "proc_searchall"
            Else
                obj = New Object() {"*", "vstokprodukfix", "idproduk", ComId.SelectedValue}
                param.Add(obj)
                obj = New Object() {"*", "vstokproduka", "idproduk", ComId.SelectedValue}
                param.Add(obj)
                TreeGrid1.ProcName(0) = "proc_searchallEqual"
                TreeGrid1.ProcName(1) = "proc_searchallEqual"
            End If
            TreeGrid1.Params = param.ToArray
            TreeGrid1.displaycolumn.Clear()
            TreeGrid1.displaycolumn.Add("idproduk")
            TreeGrid1.displaycolumn.Add("namaproduk")
            'TreeGrid1.displaycolumn.Add("namaitem")
            TreeGrid1.displaycolumn.Add("qty")
            TreeGrid1.displaycolumn.Add("stok")
            TreeGrid1.displaycolumn.Add("iditem")
            TreeGrid1.displaycolumn.Add("namaitem")
            TreeGrid1.displaycolumn.Add("qtyitem")
            TreeGrid1.displaycolumn.Add("satuan")


            TreeGrid1.setGridPproperty("idproduk")
        ElseIf ComCariBy.Text = "Item" Then
            param.Clear()
            
            If ComId.SelectedValue.ToString = "-1" Or ComId.SelectedValue.ToString Is Nothing Then
                obj = New Object() {"distinct groupitem,namagroupitem", "vitem", "namaitem", ""}
                param.Add(obj)
                obj = New Object() {"*", "vstokaktifall", "namaitem", ""}
                param.Add(obj)
                TreeGrid1.ProcName(0) = "proc_searchall"
                TreeGrid1.ProcName(1) = "proc_searchall"
            Else
                obj = New Object() {"distinct groupitem,namagroupitem", "vitem", "iditem", ComId.SelectedValue}
                param.Add(obj)
                obj = New Object() {"*", "vstokaktifall", "iditem", ComId.SelectedValue}
                param.Add(obj)
                TreeGrid1.ProcName(0) = "proc_searchallEqual"
                TreeGrid1.ProcName(1) = "proc_searchallEqual"
            End If

            TreeGrid1.Params = param.ToArray
            TreeGrid1.displaycolumn.Clear()
            TreeGrid1.displaycolumn.Add("groupitem")
            TreeGrid1.displaycolumn.Add("namagroupitem")
            'TreeGrid1.displaycolumn.Add("namaitem")
            'TreeGrid1.displaycolumn.Add("qty")
            'TreeGrid1.displaycolumn.Add("stok")
            TreeGrid1.displaycolumn.Add("iditem")
            TreeGrid1.displaycolumn.Add("namaitem")
            TreeGrid1.displaycolumn.Add("qtyakhir")
            TreeGrid1.displaycolumn.Add("satuanjual")


            TreeGrid1.setGridPproperty("groupitem")
        End If
    End Sub

    Private Sub ComCariBy_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComCariBy.SelectedIndexChanged
        If ComCariBy.Text = "Menu" Then
            Dim dt As New DataTable
            If Utils.executeSP("proc_searchall", New Object() {"*", "rsitemmenu", "namaitemmenu", ""}, dt) Then
                If Utils.RowExist(dt) Then
                    Dim dtcom As New DataTable
                    dtcom = dt.Copy
                    For i As Integer = 0 To dt.Columns.Count - 1
                        If Not dt.Columns(i).ColumnName = "iditemmenu" Then
                            If Not dt.Columns(i).ColumnName = "namaitemmenu" Then
                                Dim col As New DataColumn
                                col = dtcom.Columns(dt.Columns(i).ColumnName)
                                dtcom.Columns.Remove(col)
                            End If
                        End If
                    Next
                    Dim r As DataRow
                    r = dtcom.NewRow
                    r(0) = -1
                    r(1) = ""
                    dtcom.Rows.InsertAt(r, 0)

                    ComId.DataSource = dtcom
                    ComId.DisplayMember = "namaitemmenu"
                    ComId.ValueMember = "iditemmenu"
                End If
            End If
        ElseIf ComCariBy.Text = "Produk" Then
            Dim dt As New DataTable
            If Utils.executeSP("proc_searchall", New Object() {"*", "rsproduk", "namaproduk", ""}, dt) Then
                If Utils.RowExist(dt) Then
                    Dim dtcom As New DataTable
                    dtcom = dt.Copy
                    For i As Integer = 0 To dt.Columns.Count - 1
                        If Not dt.Columns(i).ColumnName = "idproduk" Then
                            If Not dt.Columns(i).ColumnName = "namaproduk" Then
                                Dim col As New DataColumn
                                col = dtcom.Columns(dt.Columns(i).ColumnName)
                                dtcom.Columns.Remove(col)
                            End If
                        End If
                    Next
                    Dim r As DataRow
                    r = dtcom.NewRow
                    r(0) = -1
                    r(1) = ""
                    dtcom.Rows.InsertAt(r, 0)

                    ComId.DataSource = dtcom
                    ComId.DisplayMember = "namaproduk"
                    ComId.ValueMember = "idproduk"
                End If
            End If
        ElseIf ComCariBy.Text = "Item" Then
            Dim dt As New DataTable
            If Utils.executeSP("proc_searchall", New Object() {"*", "rsitem", "namaitem", ""}, dt) Then
                If Utils.RowExist(dt) Then
                    Dim dtcom As New DataTable
                    dtcom = dt.Copy
                    For i As Integer = 0 To dt.Columns.Count - 1
                        If Not dt.Columns(i).ColumnName = "iditem" Then
                            If Not dt.Columns(i).ColumnName = "namaitem" Then
                                Dim col As New DataColumn
                                col = dtcom.Columns(dt.Columns(i).ColumnName)
                                dtcom.Columns.Remove(col)
                            End If
                        End If
                    Next
                    Dim r As DataRow
                    r = dtcom.NewRow
                    r(0) = -1
                    r(1) = ""
                    dtcom.Rows.InsertAt(r, 0)

                    ComId.DataSource = dtcom
                    ComId.DisplayMember = "namaitem"
                    ComId.ValueMember = "iditem"
                End If
            End If

        End If
    End Sub

    Private Sub ComId_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComId.SelectedValueChanged
        If Not ComId.SelectedValue.ToString = "System.Data.DataRowView" Then
            setgrid()
        End If
    End Sub

    Private Sub MonStokSaatIniPage_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.VisibleChanged
        TreeGrid1.ProcName = New String() {"proc_searchallEqual", "proc_searchallEqual"}
        ComCariBy.SelectedIndex = 0
        setgrid()
    End Sub
End Class
